package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.ActBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleCmtBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleNoticeBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleReplyBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleShareBean;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.utils.DateUtil;
import com.uchengwang.ui.view.ExpandTextView;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/2/5.
 * 消息通知
 */

public class MsgNoticyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<CycleNoticeBean> list = new ArrayList<>();

    public MsgNoticyAdapter(Context context){
        this.context = context;
    }

    public void setList(List<CycleNoticeBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<CycleNoticeBean> getList(){
        return list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_msg_view, parent, false);
        return new MsgVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        MsgVH holder = (MsgVH) h;
        final CycleNoticeBean bean = list.get(position);
        Log.d("jjf", "活动id=="+bean.getActId());
        final CycleShareBean shareBean = bean.getShare();
        CycleCmtBean cmtBean = bean.getComment();
        CycleReplyBean replyBean = bean.getReply();
        ActBean actBean = bean.getAct();

        ImageManager.getInstance().loadImage(context, holder.msg_head, bean.getFromUserAvatar(), ImageManager.ImageType.CIRCLE);
        holder.msg_time.setText(DateUtil.getDateUtil().getTimeDateWx(bean.getCreateTime()));
        holder.msg_name.setText(bean.getFromNickName());

        if (shareBean != null){
            if (!TextUtils.isEmpty(shareBean.getShareCover())){
                holder.msg_cover.setVisibility(View.VISIBLE);
                holder.msg_content.setVisibility(View.GONE);
                ImageManager.getInstance().loadImage(context, holder.msg_cover, shareBean.getShareCover(), ImageManager.ImageType.ROUND);
                if (shareBean.getShareType() ==5){
                    //是视频类型
                    holder.msg_vidoe.setVisibility(View.VISIBLE);
                    ImageManager.getInstance().loadImage(context, holder.msg_vidoe, R.drawable.ic_video_play, ImageManager.ImageType.NARMOL);
                }else {
                    holder.msg_vidoe.setVisibility(View.GONE);
                }
            }else {
                holder.msg_cover.setVisibility(View.GONE);
                holder.msg_content.setVisibility(View.VISIBLE);
                holder.msg_content.setText(bean.getShare().getShareContent());
            }
        }else if (actBean != null){
            holder.msg_cover.setVisibility(View.VISIBLE);
            ImageManager.getInstance().loadImage(context, holder.msg_cover, actBean.getActCover(), ImageManager.ImageType.ROUND);

            holder.msg_content.setVisibility(View.GONE);
            holder.msg_vidoe.setVisibility(View.GONE);
        }else {
            holder.msg_cover.setVisibility(View.GONE);
            holder.msg_content.setVisibility(View.GONE);
            holder.msg_vidoe.setVisibility(View.GONE);
        }


        if (bean.isLikeNotice()){
            holder.msg_zan.setVisibility(View.VISIBLE);
            holder.msg_cmt.setVisibility(View.GONE);
        }else if (cmtBean!= null){
            holder.msg_zan.setVisibility(View.GONE);
            holder.msg_cmt.setVisibility(View.VISIBLE);
            holder.msg_cmt.setText(cmtBean.getComment());
        }else if (replyBean != null){
            holder.msg_zan.setVisibility(View.GONE);
            holder.msg_cmt.setVisibility(View.VISIBLE);
            holder.msg_cmt.setText("回复您: "+replyBean.getReplyContent());
        }else if (actBean != null){
            holder.msg_zan.setVisibility(View.GONE);
            holder.msg_cmt.setVisibility(View.VISIBLE);
            holder.msg_cmt.setText("活动:已报名--"+actBean.getActName());
        }else {
            holder.msg_zan.setVisibility(View.GONE);
            holder.msg_cmt.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                if (shareBean != null){
                    switch (shareBean.getShareType()){
                        case 1://享说
                        case 5://视频
                            intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                            intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/share/"+bean.getShareId());
                            break;
                    }
                }else if (bean.getActId() >0){
                    intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                    intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/act/"+bean.getActId() +"/joinlist");
                    intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                }

                if (intent != null){
                    context.startActivity(intent);
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MsgVH extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView msg_head, msg_cover, msg_zan, msg_vidoe;
        private TextView msg_name, msg_time, msg_content;
        private ExpandTextView msg_cmt;


        public MsgVH(View view) {
            super(view);
            AutoUtils.autoSize(view);
            itemView = view;

            msg_head = view.findViewById(R.id.msg_head);
            msg_cover = view.findViewById(R.id.msg_cover);
            msg_name = view.findViewById(R.id.msg_name);
            msg_time = view.findViewById(R.id.msg_time);
            msg_content = view.findViewById(R.id.msg_content);
            msg_zan = view.findViewById(R.id.msg_zan);
            msg_cmt = view.findViewById(R.id.msg_cmt);
            msg_vidoe = view.findViewById(R.id.msg_vidoe);
        }
    }
}
